package com.ss.android.image.a.a;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bytedance.common.utility.Logger;
import com.facebook.datasource.AbstractDataSource;
import com.squareup.picasso.Action;
import com.squareup.picasso.Callback;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Request;
import com.squareup.picasso.RequestCreator;
import com.squareup.picasso.Utils;
import com.ss.android.image.Image;
import com.ss.android.image.loader.GifImageLoader;
import com.ss.android.image.model.ImageInfo;

public final class PicassoImage extends AbstractDataSource<Drawable> {
    private Action action;
    private ImageView imageView;

    public PicassoImage(final Request request, final boolean justCache) {
        Context context = android.support.design.a.sContext;
        if ((context instanceof Context)) {
            Image image = request.image;
            if (image != null && (image.isGif())) {
                ImageInfo imageInfo = image.gif_image_info != null ? image.gif_image_info : null;
                if (justCache) {
                    GifImageLoader gifImageLoader = GifImageLoader.getInstance(context);
                    int v1 = 0;
                    if (imageInfo != null
                            && GifImageLoader.baseImageManager != null
                            && !android.support.design.a.isTextEmpty(gifImageLoader.mImageManager.getExternalCacheFile(imageInfo.mKey))) {
                        v1 = 1;
                    }

                    if (v1 == 0) {
                        this.imageView = null;
                        this.setFailure(new NullPointerException("Image not found in bitmap-cache."));
                        return;
                    }
                }
                GifImageLoader gifImageLoader = GifImageLoader.getInstance(context);
                GifImageLoader.GifImageHolder gifImageHolder = new GifImageLoader.GifImageHolder(this);
                if (imageInfo != null && GifImageLoader.context != null) {
                    ImageView imageView = new ImageView(GifImageLoader.context);
                    String key = imageInfo.mKey;
                    if (!android.support.design.a.isTextEmpty(key)) {
                        gifImageLoader.picassoImageHolderMap.put(key, gifImageHolder);
                        if (gifImageLoader.isAlive) {
                            gifImageLoader.clear(imageView);
                            String imageInfoKey = imageInfo != null ? imageInfo.mKey : null;
                            if (imageInfoKey == null) {
                                gifImageLoader.loadImage(imageInfoKey, imageView, null);
                            } else {
                                imageView.setTag(imageInfoKey);
                                gifImageLoader.mLoadingImageViews.put(imageView, gifImageLoader.PLACEHOLDER_OBJ);
                                gifImageLoader.mMultiAsyncLoader.addTask(imageInfoKey, imageInfo, Boolean.valueOf(true), imageView);
                            }
                        }
                        this.imageView = imageView;
                    }
                } else {
                    this.imageView = null;
                }
                return;
            }


            if (justCache && !Picasso.with(context).cached(request)) {
                this.action = null;
                this.setFailure(new NullPointerException("Image not found in bitmap-cache."));
                return;
            }

            final long startTime = System.currentTimeMillis();
            this.action = new RequestCreator(Picasso.with(context), request)
                    .networkPolicy(NetworkPolicy.NO_CACHE, new NetworkPolicy[]{NetworkPolicy.NO_STORE})
                    .into(new Callback() {
                        public final void onSuccess(Drawable drawable, Picasso.LoadedFrom loadedFrom) {
                            if (Logger.debug()) {
                                Logger.d("PicassoImage", "imageRequest = " + request.toString()
                                        + " success cost = " + (System.currentTimeMillis() - startTime) + " ms" + "   justCache=" + justCache);
                            }
                            PicassoImage.this.setResult(drawable, true);
                        }

                        public final void onError(Exception exception) {
                            if (Logger.debug()) {
                                Logger.d("PicassoImage", "imageRequest = " + request.toString()
                                        + " fail cost = " + (System.currentTimeMillis() - startTime) + " ms" + "   justCache=" + justCache);
                            }
                            PicassoImage.this.setFailure(exception);
                        }
                    });
            if (!Logger.debug()) {
                return;
            }
            Logger.d("PicassoImage", "imageRequest = " + request.toString() + " start");
        }
    }

    public final void closeResult() {
        try {
            Context context = android.support.design.a.sContext;
            if (!(context instanceof Context)) {
                return;
            }

            if (this.action != null) {
                Picasso picasso = Picasso.with(context);
                Action action = this.action;
                Utils.checkMain();
                if (action != null) {
                    action.cancel();
                    picasso.dispatcher.cancel(action);
                }
            }

            if (this.imageView == null) {
                return;
            }

            GifImageLoader gifImageLoader = GifImageLoader.getInstance(context);
            ImageView imageView = this.imageView;
            if (imageView == null) {
                return;
            }
            Object tag = imageView.getTag();
            if (!(tag instanceof String)) {
                tag = null;
            }
            if (tag == null) {
                return;
            }
            gifImageLoader.mMultiAsyncLoader.cancelTask(tag, imageView);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
}

